home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2561 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Passing a pointer to a class function
  5. Date: Thu, 18 Jan 1996 13:30:54 GMT
  6. Organization: Netcom
  7. Message-ID: <30fe4acc.66601856@nntp.ix.netcom.com>
  8. References: <Pine.SUN.3.91.960117160141.13519A-100000@sasabe.acms.arizona.edu>
  9. NNTP-Posting-Host: ix-dc8-24.ix.netcom.com
  10. X-NETCOM-Date: Thu Jan 18  5:30:38 AM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. Kenton White <jwhite@sasabe.acms.arizona.edu> wrote:
  14.  
  15. > I have a library subroutine that takes as an argument a pointer to a 
  16. > function.  I want to pass instead a pointer to a class function.  The 
  17. > class looks like this:
  18. > class device {
  19. > // ...
  20. > public:
  21. > //...
  22. > void derivs(float, complex*, complex*)
  23. > };
  24. > The subroutine expects a pointer to a function like this:
  25. > rk(void (*)(float, void*, void*))
  26. > I pass the class function like this:
  27. > device laser;
  28. > rk(void (*)(float,void*, void*))laser.derivs;
  29. > When it compiles it gives me an anachronism warning and then crashes 
  30. > with a memory fault on execution.  I have developed a temporary fix by 
  31. > declaring a seperate function in the main program called derivs that 
  32. > calls the class function derivs.  This works but is silly.  Is there a 
  33. > way to pass the pointer to the class function?
  34.  
  35. No.  Pointer to function and pointer to member function are different
  36. types and cannot be converted to one another.
  37.  
  38. The problem is that there is no mechanism to tell the user of a
  39. pointer to function that it's really a pointer to member function and
  40. must be called with an appropriate class object.
  41.  
  42. Michael M Rubenstein
  43.